package com.example.exampleOS.slice;

import com.amap.api.maps.AMap;
import com.amap.api.maps.AMapOptions;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.example.exampleOS.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.location.Location;
public class AmapSlice extends AbilitySlice {
//    private String APPID;
    private MapView mapView;
    private AMap aMap;
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_amap);
//        try {
//            APPID = getBundleManager().getBundleInfo(getBundleName(), 0).getAppId();
//            System.out.println(APPID);
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }
        initMapView();
    }
    private void initMapView() {
        AMapOptions aMapOptions = new AMapOptions();
        LatLng latLng = new LatLng(34.757975,113.665412);
        aMapOptions.camera(new CameraPosition(latLng,5,80,0));
        mapView = new MapView(this,aMapOptions);
        aMap = mapView.getMap();
        //定位·
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.setLocationSource(new LocationSource() {
            @Override
            public void activate(OnLocationChangedListener onLocationChangedListener) {
                System.out.println("设置");
                Location location = new Location(34.757975,113.665412  );
                onLocationChangedListener.onLocationChanged(location);
            }

            @Override
            public void deactivate() {

            }
        });
        //位置信息
        aMap.setOnMapLoadedListener(new AMap.OnMapLoadedListener() {
            @Override
            public void onMapLoaded() {
                // todo
//                Location myLocation = aMap.getMyLocation();
//                double latitude = myLocation.getLatitude();
//                double longitude = myLocation.getLongitude();
//                System.out.println("当前坐标"+latitude+"-"+longitude);
            }
        });
        mapView.onCreate(null);
        mapView.onResume();
        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
        mapView.setLayoutConfig(config);
        super.setUIContent(mapView);
    }


    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null) {
            mapView.onDestroy();
        }
    }
}
